import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/news_provider.dart';

/// 新闻详情页
class NewsDetailPage extends ConsumerWidget {
  const NewsDetailPage({super.key, required this.newsId});

  final String newsId;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final newsAsync = ref.watch(newsDetailProvider(newsId));

    return Scaffold(
      appBar: AppBar(title: const Text('新闻详情'), backgroundColor: Theme.of(context).colorScheme.inversePrimary),
      body: newsAsync.when(
        data: (news) {
          return SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // 分类标签
                Chip(
                  label: Text(news.category),
                  backgroundColor: _getCategoryColor(news.category),
                  labelStyle: const TextStyle(color: Colors.white),
                ),
                const SizedBox(height: 16),

                // 标题
                Text(news.title, style: Theme.of(context).textTheme.headlineMedium?.copyWith(fontWeight: FontWeight.bold)),
                const SizedBox(height: 8),

                // 发布时间
                Text('发布时间: ${_formatDate(news.publishedAt)}', style: Theme.of(context).textTheme.bodySmall?.copyWith(color: Colors.grey)),
                const SizedBox(height: 24),

                // 内容
                Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Text(news.content, style: const TextStyle(fontSize: 16, height: 1.6)),
                  ),
                ),
                const SizedBox(height: 16),

                // 操作按钮
                Row(
                  children: [
                    Expanded(
                      child: OutlinedButton.icon(
                        onPressed: () {
                          // 分享功能
                          ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('分享功能演示')));
                        },
                        icon: const Icon(Icons.share),
                        label: const Text('分享'),
                      ),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: OutlinedButton.icon(
                        onPressed: () {
                          // 收藏功能
                          ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('收藏功能演示')));
                        },
                        icon: const Icon(Icons.favorite_border),
                        label: const Text('收藏'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          );
        },
        loading: () => const Center(child: CircularProgressIndicator()),
        error: (error, stack) => Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Icon(Icons.error, size: 64, color: Colors.red),
              const SizedBox(height: 16),
              Text('加载失败: $error'),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  ref.invalidate(newsDetailProvider(newsId));
                },
                child: const Text('重试'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Color _getCategoryColor(String category) {
    switch (category) {
      case '科技':
        return Colors.blue;
      case '财经':
        return Colors.green;
      case '生活':
        return Colors.orange;
      default:
        return Colors.grey;
    }
  }

  String _formatDate(DateTime date) {
    return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')} '
        '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
  }
}
